package org.tony.spider.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Properties;
import java.util.Stack;

public class FileUtil
{

  public static void writeUnDownChpaters(Stack<String> chapters, String filePath)
  {
    ObjectOutput oos = null;
    OutputStream fos = null;
    File file = new File(filePath);
    try
    {
      if (!file.exists())
      {
        file.getParentFile().mkdirs();
        file.createNewFile();
      }
      fos = new FileOutputStream(file);
      oos = new ObjectOutputStream(fos);
      oos.writeObject(chapters);
    }
    catch (Exception e)
    {
      System.err.println(e);
    }
    finally
    {
      try
      {
        fos.close();
        oos.close();
      }
      catch (Exception e)
      {

      }
    }
  }

  @SuppressWarnings({ "unchecked", "resource" })
  public static <T> T readFile(String filePath, Class<T> clazz)
  {
    File file = new File(filePath);
    if (!file.exists())
    {
      return null;
    }
    ObjectInput foi = null;
    try
    {
      foi = new ObjectInputStream(new FileInputStream(file));
      Object obj = foi.readObject();
      return (T)obj;
    }
    catch (Exception e)
    {
    }
    finally
    {
      if (foi != null)
      {
        try
        {
          foi.close();
        }
        catch (IOException e)
        {
        }
      }
    }
    return null;
  }

  public static void writeProps(String filePath, String key, String value)
  {
    URL fileURL = Thread.currentThread().getContextClassLoader().getResource(filePath);
    OutputStream fos = null;
    InputStream fis = null;
    File file = null;
    if (fileURL != null)
    {
      try
      {
        file = new File(fileURL.toURI());
        Properties pro = new Properties();
        fis = new FileInputStream(file);
        pro.load(fis);
        fis.close();
        fos = new FileOutputStream(file);
        pro.setProperty(key, value);
        pro.store(fos, String.format("add or modified %s property", key));
        fos.close();
      }
      catch (IOException e)
      {
        // load data error
        e.printStackTrace();
      }
      catch (URISyntaxException e)
      {
        // error log
        e.printStackTrace();
      }

    }
  }

  public static void storeProps(Properties props, String filePath)
  {
    OutputStream fos = null;
    InputStream fis = null;
    File file = null;
    try
    {
      file = new File(filePath);
      if (!file.exists())
      {
        file.getParentFile().mkdirs();
        file.createNewFile();
      }
      Properties pro = new Properties();
      fis = new FileInputStream(file);
      pro.load(fis);
      fis.close();
      fos = new FileOutputStream(file);
      pro.putAll(props);
      pro.store(fos, "add or modified %s property");
      fos.close();
    }
    catch (IOException e)
    {
      // load data error
      e.printStackTrace();
    }

  }

  public static void main(String[] args)
  {
    //    Stack<String> s = readFile("D:\\data\\都市少帅\\un_download_chapter.data", Stack.class);
//    System.out.println(StringUtils.getCommonPrefix(s.pop(), "第25555章 报复（第三更）（7月30日）"));
    //    System.out.println("第25555章 报复（第三更）（7月30日）".replaceAll("\u7B2C.+\u7AE0\\s+", ""));
    System.out.println("第25555章 报复（第三更）（7月30日）".replaceAll("[\\(|（].+[\\)|）]", ""));
  }
}
